<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    年份:
    <input type="text" id="year">
    月份:
    <input type="text" id="month">
    <input type="button" value="查询" id="search">
    <script>
        function $(id){
            return document.getElementById(id);
        }
        function checkYear(obj){
            var reg=/^\d{4}$/;
            if(!reg.test($("year").value)){
                $(obj).style.color="red";
                $(obj).value="输入错误,请重新输入!";
                return false;
            }
            return true;
        }
        function checkMonth(obj){
            var reg=/^(0?[1-9])|(1[0-2])$/;
            if(!reg.test($("month").value)){
                $(obj).style.color="red";
                $(obj).value="输入错误,请重新输入!";
                return false;
            }
            return true;
        }
        $("search").onclick=function(){
            console.log("hello");
            return checkYear("year") && checkMonth("month");
        }
        function focus(obj){
        $(obj).onfocus=function(){
            $(obj).style.color="black";
            $(obj).value="";
        }
    }
    focus("year");
    focus("month");
    </script>
</body>
</html>